package com.koron.sinoprobe.web.ledger.filter;

import com.alibaba.fastjson.JSON;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.util.CommonUtils;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProjectConclude;
import com.koron.sinoprobe.web.ledger.bean.vo.SpProjectVO;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: ConcludeFilter
 * @projectName GDH_EAM_V2
 * @description: 项目办结过滤器
 * @date 2022/2/1114:37
 */
@Component
public class ConcludeFilter {

    @Autowired
    private SystemFeignService systemFeignService;

    @Autowired
    private SpProjectService projectService;

    /**
     * 选人组件过滤: 主办部门分管副总审批（可选择的人），所有人都可以选择
     * @param factory
     * @param submitterParam
     * @return
     */
    @TaskAnnotation("task1Handler")
    public List<WorkflowUser> task1Handler(SessionFactory factory, SubmitterParam submitterParam) {
        return filter(factory, account(submitterParam), postCode(submitterParam), orgs(submitterParam));
    }

    /**
     * 主办部门总经理审核
     * @param factory
     * @param submitterParam
     * @return
     */
    @TaskAnnotation("task1Submitter")
    public List<WorkflowUser> task1Submitter(SessionFactory factory, SubmitterParam submitterParam) {
        SpProjectConclude bean = JSON.parseObject(submitterParam.getFormVariables(), SpProjectConclude.class);
        SpProjectVO vo = projectService.selectOne(factory, bean.getProjectId());
        // 主办部门id（岗位在插件中配置）
        List<WorkflowUser> users = filter(factory, account(submitterParam), postCode(submitterParam), Arrays.asList(vo.getHostDeptId()));
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        return users;
    }

    /**
     * 选人组件过滤: 技术部专业管理审批（可选择的人），所有人都可以选择
     * @param factory
     * @param submitterParam
     * @return
     */
    @TaskAnnotation("task3Handler")
    public List<WorkflowUser> task3Handler(SessionFactory factory, SubmitterParam submitterParam) {
        return filter(factory, account(submitterParam), postCode(submitterParam), orgs(submitterParam));
    }

    /**
     * 技术部负责人审批: 只发送至生产技术部的部门总经理
     * @param factory
     * @param submitterParam
     * @return
     */
    @TaskAnnotation("task3Submitter")
    public List<WorkflowUser> task3Submitter(SessionFactory factory, SubmitterParam submitterParam) {
        // 岗位和组织在插件中配置
        List<WorkflowUser> users = filter(factory, account(submitterParam), postCode(submitterParam), orgs(submitterParam));
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        return users;
    }

    /**
     * 获取岗位
     * @param submitterParam
     * @return
     */
    private List<String> postCode(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 岗位编码
        return users.stream().filter(row -> row.getType() == 2).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 人员
     * @param submitterParam
     * @return
     */
    private List<String> account(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 人员账号
        return users.stream().filter(row -> row.getType() == 0).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 组织
     * @param submitterParam
     * @return
     */
    private List<String> orgs(SubmitterParam submitterParam) {
        List<WorkflowUser> users = submitterParam.getUsers();
        if(CollectionUtils.isEmpty(users)) {
            return new ArrayList<>();
        }
        // 人员组织
        return users.stream().filter(row -> row.getType() == 1).map(WorkflowUser::getCode).collect(Collectors.toList());
    }

    /**
     * 根据岗位和组织查询审批人
     * 人员列表 && 组织列表 && 岗位列表
     * @param factory 会话
     * @param accounts 人员列表
     * @param orgList 组织列表
     * @param postCodes 岗位列表
     * @return
     */
    private List<WorkflowUser> filter(SessionFactory factory, List<String> accounts, List<String> postCodes, List<String> orgList) {
        if(CollectionUtils.isEmpty(postCodes) && CollectionUtils.isEmpty(orgList)) {
            return null;
        }
        PostUserQuery query = new PostUserQuery();
        query.setPostIdList(postCodes);
        query.setOrgIdList(orgList);
        List<PostUserVo> vos = CommonUtils.ok(systemFeignService.queryPostList(query));
        List<WorkflowUser> users = new ArrayList<>();
        vos.stream().forEach(vo -> {
            WorkflowUser user = new WorkflowUser();
            if(!CollectionUtils.isEmpty(accounts) && !accounts.contains(user.getCode())) {
                return;
            }
            user.setCode(vo.getUserAccount());
            user.setName(vo.getUserName());
            users.add(user);
        });
        if (users.size() == 0) {
            return null;
        }
        return users;
    }
}
